local nya__lieren = fk.CreateSkill {

  name = "nya__lieren",

  tags = {  },

}



nya__lieren:addEffect(fk.TargetSpecified, {
  name = "nya__lieren",
  anim_type = "control",
  events = {fk.TargetSpecified, fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(nya__lieren.name) then
      if event == fk.TargetSpecified then
        if data.card and data.card.trueName == "slash" and not player:isKongcheng() then
          local to = player.room:getPlayerById(data.to)
          return not to.dead and not to:isKongcheng()
        end
      else
        return player.phase == Player.Play and not player:isKongcheng() and
          player:usedSkillTimes("nya__play", Player.HistoryTurn) > 0 and not player:hasSkill("nya__play", true) and
          table.find(player.room:getOtherPlayers(player), function(p) return not p:isKongcheng() end)
      end
    end
  end,
  on_trigger = function(self, event, target, player, data)
    if event == fk.TargetSpecified then
      self:doCost(event, target, player, data.to)
    else
      self:doCost(event, target, player, data)
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if event == fk.TargetSpecified then
      if room:askForSkillInvoke(player, nya__lieren.name, nil, "#nya__lieren-invoke::"..data) then
        room:doIndicate(player.id, {data})
        return true
      end
    else
      local to = room:askForChoosePlayers(player, table.map(table.filter(room:getOtherPlayers(player), function(p)
        return not p:isKongcheng() end), function(p) return p.id end),
        1, 1, "#nya__lieren-choose", nya__lieren.name, true)
      if #to > 0 then
        event:setCostData(self, to[1])
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.TargetSpecified then
      local to = room:getPlayerById(data)
      local pindian = player:pindian({to}, nya__lieren.name)
      if room:getCardArea(pindian.results[to.id].toCard) == Card.DiscardPile then
        room:delay(1000)
        room:obtainCard(player, pindian.results[to.id].toCard, true, fk.ReasonJustMove)
      end
      if pindian.results[to.id].winner == player and not player.dead and not to.dead and not to:isNude() then
        local id = room:askForCardChosen(player, to, "he", nya__lieren.name)
        room:obtainCard(player, id, false, fk.ReasonPrey)
      end
    else
      self:use(fk.TargetSpecified, player, player, event:getCostData(self))
    end
  end,
})
nya__lieren:addEffect(fk.EventPhaseStart, {
  name = "nya__lieren",
  anim_type = "control",
  events = {fk.TargetSpecified, fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(nya__lieren.name) then
      if event == fk.TargetSpecified then
        if data.card and data.card.trueName == "slash" and not player:isKongcheng() then
          local to = player.room:getPlayerById(data.to)
          return not to.dead and not to:isKongcheng()
        end
      else
        return player.phase == Player.Play and not player:isKongcheng() and
          player:usedSkillTimes("nya__play", Player.HistoryTurn) > 0 and not player:hasSkill("nya__play", true) and
          table.find(player.room:getOtherPlayers(player), function(p) return not p:isKongcheng() end)
      end
    end
  end,
  on_trigger = function(self, event, target, player, data)
    if event == fk.TargetSpecified then
      self:doCost(event, target, player, data.to)
    else
      self:doCost(event, target, player, data)
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if event == fk.TargetSpecified then
      if room:askForSkillInvoke(player, nya__lieren.name, nil, "#nya__lieren-invoke::"..data) then
        room:doIndicate(player.id, {data})
        return true
      end
    else
      local to = room:askForChoosePlayers(player, table.map(table.filter(room:getOtherPlayers(player), function(p)
        return not p:isKongcheng() end), function(p) return p.id end),
        1, 1, "#nya__lieren-choose", nya__lieren.name, true)
      if #to > 0 then
        event:setCostData(self, to[1])
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.TargetSpecified then
      local to = room:getPlayerById(data)
      local pindian = player:pindian({to}, nya__lieren.name)
      if room:getCardArea(pindian.results[to.id].toCard) == Card.DiscardPile then
        room:delay(1000)
        room:obtainCard(player, pindian.results[to.id].toCard, true, fk.ReasonJustMove)
      end
      if pindian.results[to.id].winner == player and not player.dead and not to.dead and not to:isNude() then
        local id = room:askForCardChosen(player, to, "he", nya__lieren.name)
        room:obtainCard(player, id, false, fk.ReasonPrey)
      end
    else
      self:use(fk.TargetSpecified, player, player, event:getCostData(self))
    end
  end,
})

return nya__lieren